package com.seecen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.mapper.UserMapper;
import com.seecen.pojo.Address;
import com.seecen.pojo.User;
import com.seecen.service.AddressService;
import com.seecen.mapper.AddressMapper;
import com.seecen.until.Result;
import com.seecen.until.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
* @author 16235
* @description 针对表【t_address(地址表)】的数据库操作Service实现
* @createDate 2025-03-19 15:55:46
*/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
    implements AddressService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AddressMapper addressMapper;

    // 查所有地址
    @Override
    public Result findAllAddress(Long id) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUid, id);
        if(id==null){
            // 若用户不存在，则返回错误结果
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        List<Address> addressList = addressMapper.selectList(wrapper);
        return Result.ok(addressList);
    }

    // 充钱
    @Override
    public Result addMoney( Long id, Integer money) {
        // 创建一个LambdaQueryWrapper实例，用于构建查询条件
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getId, id); // 设置查询条件：id相等
        // 根据条件查询用户
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            // 若用户不存在，则返回错误结果
            return Result.build(null, ResultCodeEnum.USERNAME_NOTEXIST);
        }

        // 将amount与money相加，并更新用户的amount字段
        user.setAmount(user.getAmount() + money);
        userMapper.updateById(user); // 根据id更新用户信息

        // 返回成功结果，可能包含更新后的用户信息或其他相关数据
        return Result.ok("充值成功");
    }

    // 更新地址数据
    @Override
    public Result updateAddress(Address address) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getId, address.getId());
        int update = addressMapper.update(address, wrapper);
        return Result.ok(update);
    }
    // 删除地址数据
    @Override
    public Result deleteAddress(long id) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getId, id);
        int delete = addressMapper.delete(wrapper);
        return Result.ok(delete);
    }
    // 添加地址数据
    @Override
    public Result addAddress(Address address) {
        try {
            // 1. 参数校验
            if (address.getUid() == null || address.getUid() <= 0) {
                return Result.build("用户ID不合法",ResultCodeEnum.CODE_ERROR);

            }
            if (StringUtils.isBlank(address.getUsername())) {
                return Result.build("收件人不能为空",ResultCodeEnum.CODE_ERROR);
            }
            if (StringUtils.isBlank(address.getAddress())) {
                return Result.build( "详细地址不能为空",ResultCodeEnum.CODE_ERROR);
            }
            // 3. 保存地址
            int result = addressMapper.insert(address);
            if (result <= 0) {
                return Result.build("地址保存失败",ResultCodeEnum.CODE_ERROR);
            }

            // 4. 返回新增地址的ID
            return Result.ok(address.getId());
        } catch (Exception e) {
            log.error("添加地址失败：{}");
            return Result.build("系统异常",ResultCodeEnum.CODE_ERROR);
        }

    }
}




